#include "MyEEPROM.h"

MyEEPROM::MyEEPROM()
{
    EEPROM.begin(1024);
}


/**
 * @brief 读取背光亮度
 *
 * @return uint8_t
 */
uint8_t MyEEPROM::readLCDBacklightConfig()
{
    this->LCD_BL_PWM = EEPROM.read(_BL_addr);
    if (LCD_BL_PWM >= 0 && LCD_BL_PWM <= 255)
        return LCD_BL_PWM;
    else
        return 100;
}


/**
 * @brief 读取屏幕方向
 *
 * @return uint8_t
 */
uint8_t MyEEPROM::readLCDRotationConfig()
{
    this->LCD_Rotation = EEPROM.read(_Ro_addr);
    if (LCD_Rotation >= 0 && LCD_Rotation <= 3)
        return LCD_Rotation;
    else
        return 100;
}


/**
 * @brief 读取滚动条滚动间隔
 *
 * @return uint8_t
 */
uint8_t MyEEPROM::readScrollIntervalConfig()
{
    this->scrollInterval = EEPROM.read(_scroll_interval_addr);
    if (scrollInterval > 0 && scrollInterval <= 10)
        return scrollInterval;
    else
        return 2;
}


/**
 * @brief 读取天气更新间隔
 *
 * @return uint8_t
 */
uint8_t MyEEPROM::readUpadteWeatherTimeConfig()
{
    this->updateWeatherTime = EEPROM.read(_update_weather_time_addr);
    if (updateWeatherTime >= 1 && updateWeatherTime <= 60)
        return updateWeatherTime;
    else
        return 10;
}


/**
 * @brief 读取动画帧率
 *
 * @return uint8_t
 */
uint8_t MyEEPROM::readAnimateFrameRateConfig()
{
    this->animateFrameRate = EEPROM.read(this->_animate_frame_rate_addr);
    if (animateFrameRate >= 1 && animateFrameRate <= 60)
        return animateFrameRate;
    else
        return 20;
}


/**
 * @brief 读取城市代码
 *
 * @return uint32_t
 */
uint32_t MyEEPROM::readCityConfig()
{
    uint32_t cityCode = 0;
    for (int cnum = 5; cnum > 0; cnum--)
    {
        cityCode = cityCode * 100;
        cityCode += EEPROM.read(_CC_addr + cnum - 1);
        delay(5);
    }
    return cityCode;
}


/**
 * @brief 读取WIFI信息
 *
 * @return WifiConfigStructure
 */
WifiConfigStructure MyEEPROM::readWIFIConfig()
{
    uint8_t *p = (uint8_t *)(&wifiConf);
    for (unsigned int i = 0; i < sizeof(wifiConf); i++)
    {
        *(p + i) = EEPROM.read(i + _wifi_addr);
    }
    return wifiConf;
}


/**
 * @brief 保存背光亮度
 *
 * @param LCDBackLight
 */
void MyEEPROM::saveLCDBacklightConfig(uint8_t LCDBackLight)
{
    if (LCDBackLight != this->readLCDBacklightConfig()) // 避免重复写入
    {
        if (LCDBackLight <= 255 and LCDBackLight >= 0)
            EEPROM.write(_BL_addr, LCDBackLight);
        else
        {
            EEPROM.write(_BL_addr, 80);
        }
        EEPROM.commit();
        delay(5);
    }
}


/**
 * @brief 保存屏幕方向
 *
 * @param LCDRotation
 */
void MyEEPROM::saveLCDRotationConfig(uint8_t LCDRotation)
{
    if (LCDRotation != this->readLCDRotationConfig())
    {
        if (LCDRotation <= 3 and LCDRotation >= 0)
            EEPROM.write(_Ro_addr, LCDRotation);
        else
        {
            EEPROM.write(_Ro_addr, 0);
        }
        EEPROM.commit();
        delay(5);
    }
}


/**
 * @brief 保存滚动条滚动间隔
 *
 * @param scrollInterval
 */
void MyEEPROM::saveScrollIntervalConfig(uint8_t scrollInterval)
{
    if (scrollInterval != this->readScrollIntervalConfig())
    {
        if (scrollInterval > 0 && scrollInterval <= 10)
            EEPROM.write(this->_scroll_interval_addr, scrollInterval);
        else
            EEPROM.write(this->_scroll_interval_addr, 2);
        EEPROM.commit();
        delay(5);
    }
}


/**
 * @brief 保存动画帧率
 *
 * @param animateFrameRate
 */
void MyEEPROM::saveAnimateFrameRateConfig(uint8_t animateFrameRate)
{
    if (animateFrameRate != this->readAnimateFrameRateConfig())
    {
        if (animateFrameRate > 0 && animateFrameRate <= 60)
            EEPROM.write(this->_animate_frame_rate_addr, animateFrameRate);
        else
            EEPROM.write(this->_animate_frame_rate_addr, 20);
        EEPROM.commit();
        delay(5);
    }
}


/**
 * @brief 保存天气更新间隔
 *
 * @param updateWeatherTime
 */
void MyEEPROM::saveUpdateWeatherTimeConfig(uint8_t updateWeatherTime)
{
    if (updateWeatherTime != this->readUpadteWeatherTimeConfig())
    {
        if (updateWeatherTime >= 1 && updateWeatherTime <= 60)
            EEPROM.write(_update_weather_time_addr, updateWeatherTime);
        else
        {
            EEPROM.write(_update_weather_time_addr, 10);
        }
        EEPROM.commit();
        delay(5);
    }
}


/**
 * @brief 保存城市代码
 *
 * @param cityCode
 */
void MyEEPROM::saveCityConfig(uint32_t cityCode)
{
    if (cityCode != this->readCityConfig())
        if (((cityCode >= 101000000) && (cityCode <= 102000000)) || (cityCode == 0))
        {
            for (int cnum = 0; cnum < 5; cnum++)
            {
                EEPROM.write(_CC_addr + cnum, cityCode % 100); //城市地址写入城市代码
                EEPROM.commit();                               //保存更改的数据
                cityCode = cityCode / 100;
                delay(5);
            }
        }
}


/**
 * @brief 保存WIFI信息
 *
 * @param wifiConfig
 */
void MyEEPROM::saveWIFIConfig(WifiConfigStructure *wifiConfig)
{
    //开始写入
    uint8_t *p = (uint8_t *)(wifiConfig);
    for (uint8_t i = 0; i < sizeof(*wifiConfig); i++)
    {
        EEPROM.write(i + _wifi_addr, *(p + i)); //在闪存内模拟写入
    }
    delay(10);
    EEPROM.commit(); //执行写入ROM
    delay(10);
}

void MyEEPROM::deletewificonfig()
{
    memset(wifiConf.stapsw, 0, sizeof(wifiConf.stapsw));
    memset(wifiConf.stassid, 0, sizeof(wifiConf.stassid));
    saveWIFIConfig(&wifiConf);
}
